package com.ajax;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author wzm
 * @create 2023-05-16 15:03
 */
@WebServlet("/axios02")
public class AxiosServlet02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //如果post请求传递数据的时候直接传入字符串 可以通过通用的方法获取
//        String username = req.getParameter("username");
//        System.out.println("username = " + username);

        //如果传入的是字符串 则无法通过通用的方法获取 则需要使用获取请求体的方法获取
        BufferedReader reader = req.getReader();
        String s = reader.readLine();
        System.out.println("s = " + s);

        //格式为
//       s = "{\"username\":\"张三\",\"age\":18}";


        resp.getWriter().write("恭喜你，访问成功");

    }
}
